﻿using UnityEngine;
using System.Collections;

public class TouchControls : MonoBehaviour {
	bool Selected = false;
	private int selectedFingerId = -1;

	// Use this for initialization
	public virtual void Start () {
	}
	//Default Tap behaviour
	public virtual void Tapped ()
	{
		Destroy (this.gameObject);
	}
	//Default drag behaviour
	public virtual void Drag (Vector2 pos)
	{
		this.transform.position = new Vector3 (pos.x, pos.y, this.transform.position.z);
	}

	//Dont Override
	public virtual void  Touched(Vector2 pos, TouchPhase phase, int fingerId)
	{
		if (phase == TouchPhase.Began && this.collider2D.OverlapPoint (pos)) {
						Selected = true;
						selectedFingerId = fingerId;
						Tapped();
				} else if (phase == TouchPhase.Ended && selectedFingerId == fingerId) {
					Selected = false;
					selectedFingerId = -1;
				}
		//Object being Dragged
		if (Selected && selectedFingerId == fingerId) {
			Drag(pos);
				}
	}
	void OnMouseDown()
	{
		Tapped ();
	}
	public virtual void OnMouseDrag()
	{
		Drag (Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 0)));
	}
	// Update is called once per frame
	void Update () {
	}
}
